package com.qf.fanwang.factory;

import java.io.IOException;
import java.util.Properties;

public class MyFactory {
    private Properties properties = new Properties();

    public MyFactory() {
    }

    public void setProperties(String config) {
        try {
            properties.load(MyFactory.class.getClassLoader().getResourceAsStream(config));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public Object getBean(String bean) {//配置文件中键值名称
        String classPath = properties.getProperty(bean);
        if (classPath != null) {
            Class clazz = null;
            //反射：加载类对象
            try {
                clazz = Class.forName(classPath);
                //反射：获取类对象
                return clazz.newInstance();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}
